<template>
  <div>

    <!--搜索-->
    <div style="margin: 10px 0">
      <el-input v-model="name" placeholder="请输入名称" style="width: 200px" suffix-icon="el-icon-search"
                clearable></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
      <el-button type="warning" style="margin-left: 5px" @click="reset">重置</el-button>
    </div>
    <!--编辑-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5 mr-5"
          confirm-button-text='我已经下定了决心'
          cancel-button-text='不，不要。不要啊'
          icon="el-icon-info"
          icon-color="red"
          title="这可是一堆数据哥们，好好思考一下？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>
    <!--          表格数据-->
    <el-table :data="tableData" border stripe @selection-change="handleSelectionChange"
              row-key="id" default-expand-all
              :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column label="ID" prop="id" width="80"></el-table-column>
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="路径" prop="path"></el-table-column>
      <el-table-column label="页面路径" prop="pagePath"></el-table-column>
      <el-table-column label="图标" align="center">
        <template #default="scope">
          <i :class="scope.row.icon" style="font-size: 20px"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" prop="description"></el-table-column>
      <el-table-column label="操作" prop="handle" width="300px">
        <template #default="scope">
          <el-button type="primary" @click="handAddMenu(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单
            <i class="el-icon-plus"></i></el-button>
          <el-button type="success" @click="handEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确认删除'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="Are you Sure？"
              @confirm="handDelete(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-delete"></i>
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>
    <div>
      <el-dialog title="菜单信息" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="form" label-width="80px" size="small">
          <el-form-item label="名称">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="路径">
            <el-input v-model="form.path" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="页面路径">
            <el-input v-model="form.pagePath" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="图标">
            <el-select clearable v-model="form.icon" placeholder="请选择" style="width: 100%">
              <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
                <i :class="item.value"/> {{item.name}}
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.description" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!--          弹窗-->
  </div>
</template>

<script>
export default {
  name: "User",
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      name: '',
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      options: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/menu", {
        params: {
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data
      })
    },
    reset() {
      this.name = ''
      this.load()

    },
    handAddMenu(pid) {
      this.dialogFormVisible = true
      this.form = {}
      if (pid) {
        this.form.pid = pid
      }
    },
    handAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    save() {
      this.request.post("/menu", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false;
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handEdit(row) {
      this.form = row
      this.dialogFormVisible = true
      this.form = Object.assign({}, row) //点击取消后不保存数据 其实就是拷贝到一个空对象，就得到了原始对象的克隆，不进行修改，返回原来的对象
      // 请求图标的数据
      this.request.get("/menu/icons").then((res) => {
        this.options = res.data
      })
    },
    handDelete(id) {
      this.request.delete("/menu/" + id).then((res) => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val); //val是一个对象
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(value => value.id) // [{},{}] => [1,2...]
      this.request.post("/menu/del/batch", ids).then((res) => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.currentPage = pageNum
      this.load()
    },
    exp() {
      window.open("http://localhost:9090/menu/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    },
  }
}
</script>

<style scoped>

</style>